const WXAPI = require('../../wxapi/main');
const con = require("../../utils/data.js");
const app = getApp()
Page({
  data: {
    addressList: [],
    mycurAddressData:[]
  },

  selectTap: function(e) {
    var id = e.currentTarget.dataset.id;
    WXAPI.updateAddress({
      token: wx.getStorageSync('token'),
      id: id,
      isDefault: 'true'
    }).then(function(res) {
      wx.navigateBack({})
    })
  },

  addAddess: function() {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  editAddess: function(e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },

  onLoad: function() {
    console.log('onLoad')


  },
  onShow: function() {
    this.initShippingAddress();
  },
  initShippingAddress: function() {
    var that = this;
    // 保存到自己的后台
    wx.request({
      url: con.index_getaddress,
      method: 'GET',
      data: {
        openid: wx.getStorageSync('OpenId'),
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        console.log('返回成功');
        that.setData({
          mycurAddressData: res.data.body
        })


      },
      fail: function (res) {
        console.log('返回失败');
      }
    })

// ---------------
    WXAPI.queryAddress(wx.getStorageSync('token')).then(function(res) {
      if (res.code == 0) {
        that.setData({
          addressList: res.data
        });
      } else if (res.code == 700) {
        that.setData({
          addressList: null
        });
      }
    })
  }

})